﻿Public Class clsLogger
    Private LogDir As String = ""
    Private swLog As IO.StreamWriter = Nothing
    Private isDisabled As Boolean = False

    Public Sub New()
        Initialize(My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData, "")
    End Sub

    Public Sub New(LogDir As String, Optional Filename As String = "")
        Initialize(LogDir, Filename)
    End Sub

    Private Sub Initialize(LogDir As String, Filename As String)
        Me.LogDir = LogDir
        Dim fn As String = IO.Path.Combine(LogDir, My.Application.Info.AssemblyName & ".log")
        If Filename <> "" Then
            fn = IO.Path.Combine(LogDir, IO.Path.ChangeExtension(Filename, ".log"))
        End If
        Try
            swLog = New IO.StreamWriter(fn)
            Write(NameOf(Initialize), "Logger file created: " & fn)
        Catch ex As Exception
            Debug.WriteLine("Unable to initialize logfile: " & ex.Message)
            isDisabled = True
        End Try
    End Sub

    Public Sub Write(MethodName As String, Message As String)
        If Not isDisabled Then
            Dim s As String = $"{Now}{vbTab}{MethodName}{vbTab}{Message}"
            swLog.WriteLine(s)
            Debug.WriteLine("Log>>>" & s)
            swLog.Flush()
        End If
    End Sub
End Class
